package face.pay.web.device.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import face.pay.entity.DeviceAssignmentInfo;
import face.pay.entity.DeviceBatchInfo;
import face.pay.entity.DeviceOrderInfo;
import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.device.bo.DeviceOrderInfoBO;
import face.pay.web.device.service.DeviceOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Classname DeviceOrderInfoController
 * @Description JJL
 * @Date 2019/11/11 13:21
 * 设备订单
 */
@Controller
@RequestMapping("/deviceOrder")
@Slf4j
public class DeviceOrderInfoController {

    @Autowired
    private DeviceOrderInfoService deviceOrderInfoService;

    @RequestMapping("/list")
    public String manager(){
        return  "device/deviceOrderInfoList";
    }

    /**
     * 目前用户查询可用设备
     * @param loginUser
     * @param pageParam
     * @param deviceOrderInfo
     * @return
     */
    @RequestMapping("/queryData")
    @ResponseBody
    public PageData data(@CurrentUser UserInfo loginUser, PageParam pageParam,DeviceOrderInfo deviceOrderInfo){
        return deviceOrderInfoService.queryPage(loginUser,pageParam,deviceOrderInfo);
    }



    /**
     * 导入
     * @param loginUser 当前登录人信息
     * @param file 导入的文件
     * @param deviceBatchInfo 批次类型等
     */
    @PostMapping("/importExcel")
    @ResponseBody
    public ResultMessage importExcel(@CurrentUser UserInfo loginUser,
                                      @RequestParam("file") MultipartFile file,
                                      DeviceBatchInfo deviceBatchInfo) {

        if(deviceBatchInfo.getEnterType().length() == 0 || deviceBatchInfo.getEnterType() == null){
            return ResultMessage.createFail("请选择进货方式");
        }
        if(deviceBatchInfo.getDeviceType().length() == 0 || deviceBatchInfo.getDeviceType() == null){
            return ResultMessage.createFail("请选择货物类型");
        }
        if(deviceBatchInfo.getProducer().length() == 0 || deviceBatchInfo.getProducer() == null){
            return ResultMessage.createFail("请选择厂商");
        }

        ImportParams importParams = new ImportParams();
        importParams.setNeedVerfiy(true); //开启校验
        try {
            ExcelImportResult<DeviceOrderInfoBO> result = ExcelImportUtil.importExcelMore(file.getInputStream(), DeviceOrderInfoBO.class, importParams);
            if (result.getList().size() == 0) {  //模板中没有一条正确的数据时
                return ResultMessage.createFail("文件内容错误,请按照模板填写数据");
            }
            if (result.isVerfiyFail()) { //有未通过校验的数据时
                for (DeviceOrderInfoBO device: result.getFailList()) { //得到未通过校验的数据
                    return ResultMessage.createFail("数据错误,请检查第"+(device.getRowNum()+1)+"行");  //输出它的行数  +1是因为标题占了一行
                }
            }
            return deviceOrderInfoService.addDeviceOrderInfos(loginUser, deviceBatchInfo, result.getList());
        } catch (Exception e) {
            log.error("导入异常",e);
        }
        return ResultMessage.createFail("数据错误,请检查数据是否重复");
    }

    /**
     * 代理商转出设备
     * @param userId 设备接收人id
     * @param deviceOrderId 设备订单id
     * @param deviceAssignmentInfo 设备转出信息
     * @return
     */
    @PostMapping("/assignmentDevice")
    @ResponseBody
    public ResultMessage assignmentDeviceOrderInfo(@CurrentUser UserInfo loginUser, Integer userId, Integer deviceOrderId,
                                                   DeviceAssignmentInfo deviceAssignmentInfo){
        return deviceOrderInfoService.assignmentDeviceOrderInfo(loginUser,userId,deviceOrderId,deviceAssignmentInfo);
    }

}
